package vvui.net.web.commons;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kevin.framework.basic.common.utils.DataSourceInfo;
import kevin.framework.basic.common.utils.ISpringContextListener;
import kevin.framework.basic.common.utils.SysConfigParams;

public class SpringStartedListener implements ISpringContextListener {

	/**
	 * 环境就绪（不包括多数据源创建完成）
	 **/
	@Override
	public void onEnvFinished() {
	}

	@Override
	public void onBeforeEvnDestory() {
	}

	/***
	 * 设置数据源，返回DataSourceInfo列表，这里从文件读取，可以实现从数据库读取
	 ****/
	@Override
	public List<DataSourceInfo> setDataSourceList() {
		List<DataSourceInfo> dbList = new ArrayList<DataSourceInfo>();		
		String file = SysConfigParams.configHome + File.separator + "db-list.json";
		InputStream inputStream = null;
		PathMatchingResourcePatternResolver resoler = new PathMatchingResourcePatternResolver();
		try {
			inputStream = resoler.getResource(file).getInputStream();
			String text = IOUtils.toString(inputStream, "utf8");
			Map<String, Object> map = JSON.parseObject(text, Map.class);
			JSONObject props = (JSONObject) map.get("props");
			Set<Entry<String, Object>> entrySet = props.entrySet();
			
			JSONObject xaProps = (JSONObject) map.get("xaProps");
			Set<Entry<String, Object>> xaEntrySet = xaProps.entrySet();
			
			String dialect = String.valueOf(map.get("dialect"));
			JSONArray dbListTmp = (JSONArray) map.get("dbList");
			Iterator<Object> iterator = dbListTmp.iterator();
			while (iterator.hasNext()) {
				JSONObject tmp = (JSONObject) iterator.next();

				String id = tmp.getString("id");
				String url = tmp.getString("url");
				String username = tmp.getString("username");
				String password = tmp.getString("password");
				String driverClassName = tmp.getString("driverClassName");
				String name = tmp.getString("name");

				DataSourceInfo dbInfo = new DataSourceInfo();
				dbInfo.setId(id);
				dbInfo.setDriverClass(driverClassName);
				dbInfo.setDbName(name);
				dbInfo.setUrl(url);
				dbInfo.setUsername(username);
				dbInfo.setPassword(password);

				dbInfo.setProperties("dialect", dialect);
				for (Entry<String, Object> entry : entrySet) {
					dbInfo.setProperties(entry.getKey(), entry.getValue());
				}
				
				for (Entry<String, Object> entry : xaEntrySet) {
					dbInfo.setxaProps(entry.getKey(), entry.getValue());
				}
				
				dbList.add(dbInfo);
				System.out.println(">>>>>>>>>>>>>");
			}
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
				}
			}
		}
	    
		return dbList;
	}

	/**
	 * 数据源创建完成， failList：创建失败的数据源
	 ***/
	@Override
	public void onDataSourceCreated(List<DataSourceInfo> failList) {
	}

}
